
#sys.path.append "../script"


import nmap
import time
import threading

from coptur.models.host import Host
from coptur.models.site import Site
from coptur.models.port import Port
from coptur.models.url import Url
from coptur.models.poc import Poc
from coptur.models.vuln import Vuln

class Scanner(threading.Thread):

    def __init__( self, name, type, q ):
        threading.Thread.__init__(self)
        self.queuelock = threading.Lock()
        self.name = name # 扫描线程名
        self.type = type # 扫描类型，主机还是web
        self.q = q


    def run(self):
        if self.type == "host":
            self.host_scan(self.name,self.q)
        elif self.type == "web":
            self.web_scan(self.name,self.q)
        elif self.type == "port":
            self.port_scan(self.name,self.q)
        else:
            pass


    def port_scan(self,name,q):
        # 使用n个线程扫描端口
        while not q.empty():
            self.queuelock.acquire()
            target = q.get()
            self.queuelock.release()
            print("%s => %s" %(__name__, target) )
            nm = nmap.PortScanner()
            result = nm.scan(target,'1-1024')
            print(result)


    def host_scan(self,name,q):
        # 使用n个脚本遍历扫描q
        while not q.empty():
            self.queuelock.acquire()
            if not q.empty():
                target = q.get()
                self.queuelock.release()
                print("%s => %s" %(__name__, target) )
                # results from crawler, put to queue
                #_url = url+"."

                #self.queuelock.acquire()
                #q.put(_url)
                #self.queuelock.release()

                print ("%s processing %s" % (name, target))
            else:
                self.queuelock.release()
            time.sleep(1)




    def web_scan(self,name,q):
        # 使用n个脚本遍历扫描q
        pass
